package com.example.mvp.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.v4.content.ContextCompat;

import com.example.BaseNewApplication;
import com.example.baselibrary.R;
import com.example.mvp.binder.BaseDataBinder;
import com.example.mvp.delegate.XDelegate;

import com.example.mvp.view.BtLoading;
import com.example.util.SoftKeyBoardUtilKt;

import cc.vv.lkbasecomponent.base.ui.LKBaseActivity;
import cc.vv.lkbasecomponent.base.ui.loading.InterLoading;
import cc.vv.lkbasecomponent.base.ui.loading.LKBaseLoading;

/**
 * MVP模式的Activity基类
 * Created by sun.li on 2018/6/5.
 *
 * @author sun.li
 */

public abstract class BaseActivityMVP<T extends XDelegate> extends LKBaseActivity {

    /** 视图托管类对象*/
    protected T viewDelegate;

    /** 公共数据视图绑定控制器对象*/
    protected BaseDataBinder binder;

    protected Context mContext;
    /**
     * Loading
     */
    private LKBaseLoading mLoadingView;
    public BaseActivityMVP(){
        try {
            viewDelegate = getDelegateClass().newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("create IDelegate error");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create IDelegate error");
        }
    }

    @CallSuper
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.bkF7F4F4color));
        BaseNewApplication.getInstance().addActivity(this);
//        ConnectionMonitor.Companion.getInstance().setContext(this);
        //StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.color_FFFFFF), false);
        mContext = this;
        try {
            viewDelegate.create(getLayoutInflater(), null);
            setContentView(viewDelegate.getRootView());

        }catch (Exception e){

        }
        initDate();
        binder = getDataBinder();
        super.onCreate(savedInstanceState);
        viewDelegate.initWidget();
        bindEvenListener();

    }

    /** 初始化界面数据*/
    protected abstract void initDate();

    /** 注册界面控件点击事件*/
    protected abstract void bindEvenListener();

    /** 指定界面的视图控制器，必须指定*/
    protected abstract Class<T> getDelegateClass();

    /** 公共数据视图绑定控制器数据对象*/
    public abstract BaseDataBinder getDataBinder();

    /** 根据数据刷新视图*/
    public <D extends Object> void notifyModelChanged(D data) {
        if (binder != null) {
            binder.viewBindModel(viewDelegate, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /* 界面关闭解除视图绑定操作*/
        viewDelegate = null;
        BaseNewApplication.getInstance().removeActivity(this);
    }

    /**
     * 重写此方法，自定义Loading
     *
     * @return
     */
    @Override
    public InterLoading getLoading() {
        if (null == mLoadingView) {
            mLoadingView = new BtLoading(R.layout.view_bt_loading);
        }
        return mLoadingView;
    }

      public void showLoading(){
        if (mLoadingView!=null&&!mLoadingView.isShowing()){
            mLoadingView.showLoading();
        }

      }
    public void closeLoading(){
        if (mLoadingView!=null){
            mLoadingView.closeLoading();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        // 隐藏键盘
        try {
            SoftKeyBoardUtilKt.closeInput(this, getWindow().getDecorView());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
